<?php

/**
 * ECTouch Open Source Project
 * ============================================================================
 * Copyright (c) 2012-2014 http://ectouch.cn All rights reserved.
 * ----------------------------------------------------------------------------
 * 文件名称：OrderModel.class.php
 * ----------------------------------------------------------------------------
 * 功能描述：ECTOUCH 订单退款./退换货模型
 * ----------------------------------------------------------------------------
 * Licensed ( http://www.ectouch.cn/docs/license.txt )
 *type-1-退款-2-退货-3-换货-0-异常，status-0-异常-1-待审核-2-审核通过-3-审核不通过-4-审核已结束
 * ----------------------------------------------------------------------------
 */

/* 访问控制 */
defined('IN_ECTOUCH') or die('Deny Access');

class OrderReturnModel extends BaseModel {
        /**
     * 取得信息
     * @param   int     $order_id    订单ID
     * @return     订单信息
     */
    function get_info_by_return_id($return_id) {
        $sql = "SELECT * "
               ." FROM " . $this->pre . "order_return where return_id=".$return_id;

        $row = $this->row($sql);
        
        return $row;
    }
    /**
     * 取得订单信息
     * @param   int     $order_id    订单ID
     * @return     订单信息
     */
    function get_order_info($order_id) {
        $sql = "SELECT order_id,order_sn,user_id,money_paid,shipping_fee,order_status,shipping_status,pay_status"
        .",return_status,return_id FROM " . $this->pre . "order_info where order_id=".$order_id;

        $row = $this->row($sql);
        
        return $row;
    }
        /**
     * 取得订单信息
     * @param   int     $order_id    订单ID
     * @return     订单信息
     */
    function get_order_by_return_id($return_id) {
        $sql = "SELECT order_id,return_id,return_status,order_sn,user_id,money_paid,shipping_fee,order_status,shipping_status,pay_status"
               ." FROM " . $this->pre . "order_info where return_id=".$return_id;

        $row = $this->row($sql);
        
        return $row;
    }
    /**
     * 取得待退款的申请总金额
     * @param   int     $user_id    买家
     * @return     订单信息
     */
    function get_sum_return_fee($user_id,$where_str) {
        $sql = "SELECT sum(request_fee) as sum_request_fee"
        ." FROM " . $this->pre . "order_return where status=1 and  user_id=".$user_id.$where_str;
        $row = $this->row($sql);
        
        return $row;
    }
    /**
     * 插入数据
     * @return     处理结果
     */
    function insert_return($da) {


        $now_time=time();
        $sql="insert into ".$this->pre . "order_return(`order_id`,`order_sn`,`user_id`,`money_paid`,`shipping_fee`,`request_fee`"
            .",`remark`,`type`,`ctime`,`img`,`number`,`status`) values('$da[order_id]','$da[order_sn]','$da[user_id]','$da[money_paid]','$da[shipping_fee]'"
            .",'$da[request_fee]','$da[remark]','$da[type]','$now_time','$da[img]','$da[number]','1')";

        $res = $this->query($sql);
        $return_id=mysql_insert_id();
        if($res)
        {
            $sql="update ".$this->pre . "order_info set return_status='$da[type]',return_id='$return_id' where order_id=$da[order_id]";
            $res2 = $this->query($sql);
        }
        return  $res;

    }


      /**
     * 查询退款、退换货的申请记录
     * @param   int     $user_id    会员ID
     * @param   int     $num        每页显示数量
     * @param   int     $start      开始显示的条数
     * @return  array
     */
    function get_return_list($user_id, $num, $start,$where_str="") {
        $return_list = array();
        $sql = 'SELECT * FROM ' . $this->pre . "order_return WHERE user_id = '$user_id'".$where_str .
                " ORDER BY ctime DESC limit " . $start . ',' . $num;
        $list = $this->query($sql);

     

        if (is_array($list)) {
            foreach ($list as $vo) {
                $vo['ctime'] = local_date(C('date_format'), $vo['ctime']);
                $vo['remark'] = nl2br(htmlspecialchars($vo['remark']));
                $vo['short_remark'] = ($vo['remark'] > '') ? sub_str($vo['remark'], 30) : 'N/A';
                $vo['check_remark'] = nl2br(htmlspecialchars($vo['check_remark']));
                $vo['short_check_remark'] = ($vo['check_remark'] > '') ? sub_str($vo['check_remark'], 30) : 'N/A';

                $vo['request_fee'] = price_format(abs($vo['request_fee']), false);
                $vo['fact_fee'] = price_format(abs($vo['fact_fee']), false);

                /* 会员的操作类型： */
                if ($vo['type'] == 1) {
                    $vo['type_str'] ='退款';
                } else if ($vo['type'] == 2) {
                    $vo['type_str'] ='退货';
                }
                 else if ($vo['type'] == 3) {
                    $vo['type_str'] ='换货';
                }
                /* 会员的操作状态： */
                if ($vo['status'] == 2) {
                    $vo['status_str'] ='审核通过';
                } else if ($vo['status'] == 3) {
                    $vo['status_str'] ='审核不通过';
                } 
                 else if($vo['status'] == 4) 
                {
                    $vo['status_str']='已结束';
                }
                else if($vo['status'] == 1) 
                {
                    $vo['status_str']='待审核';
                }
                else
                {
                     $vo['status_str']='异常';
                }

                $return_list[] = $vo;
            }
            return $return_list;
        } else {
            return false;
        }
    }

}
